package com.vercon.moensmart.fragment.setting

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.vercon.common.Constants
import com.vercon.common.R
import com.vercon.common.utils.LogUtils
import com.vercon.moensmart.databinding.FragmentSettingBinding
import com.vercon.moensmart.fragment.base.BaseFragment
import com.vercon.moensmart.fragment.manager.FragmentOpenManager.Companion.getInstance
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit

class SettingFragment : BaseFragment<FragmentSettingBinding?>() {
    private lateinit var title: Array<String>
    override fun onCreateViewBinding(
        inflater: LayoutInflater,
        parent: ViewGroup?
    ): FragmentSettingBinding {
        return FragmentSettingBinding.inflate(inflater, parent, false)
    }

    override fun initData() {
        title = arrayOf("WIFI")
        val fragmentList: MutableList<Fragment> = ArrayList()
        fragmentList.add(WiFiFragment())
        val myPagerAdapter = MyPagerAdapter(childFragmentManager, fragmentList)
        binding!!.viewPager.adapter = myPagerAdapter
        binding!!.viewPager.setStartAnimation(false)
        binding!!.viewPager.offscreenPageLimit = 6
        Observable.timer(300, TimeUnit.MILLISECONDS)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<Long?> {
                override fun onSubscribe(d: Disposable) {}
                override fun onNext(aLong: Long) {
                    binding!!.tabLayout.setupWithViewPager(binding!!.viewPager)
                    val arguments = arguments
                    if (arguments != null) {
                        val nums = arguments.getInt(go_item)
                        binding!!.viewPager.setCurrentItem(nums, false)
                    }
                    setUItayout()
                }

                override fun onError(e: Throwable) {}
                override fun onComplete() {}
            })
    }

    override fun initListener() {
        binding!!.igClose.setOnClickListener { v: View? ->
            getInstance().closeFragment(requireActivity())
        }
        setLayoutWidth()
    }

    private fun setUItayout() {
        val layoutParams = binding!!.tabLayout.layoutParams as RelativeLayout.LayoutParams
        layoutParams.marginStart = resources.getDimension(R.dimen.dp_450).toInt()
        layoutParams.marginEnd = resources.getDimension(R.dimen.dp_450).toInt()
        binding!!.tabLayout.layoutParams = layoutParams
    }

    override fun onClick(v: View) {}
    internal inner class MyPagerAdapter(fm: FragmentManager?, fragments: List<Fragment>) :
        FragmentPagerAdapter(
            fm!!
        ) {
        private val mWiFiFragment: WiFiFragment? = null
        private var fragmentList: List<Fragment> = ArrayList()

        //用于区分具体属于哪个fragment
        override fun setPrimaryItem(container: ViewGroup, position: Int, `object`: Any) {
            Constants.isUsershow = position == 3
            super.setPrimaryItem(container, position, `object`)
        }

        init {
            fragmentList = fragments
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return title[position]
        }

        override fun getItem(position: Int): Fragment {
            return if (fragmentList.size > 0 && fragmentList[position] != null) {
                fragmentList[position]
            } else {
                Fragment()
            }
        }

        override fun getCount(): Int {
            return title.size
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {}
    }

    fun setLayoutWidth() {
        binding!!.tabLayout.post {
            // 拿到tabLayout的slidingTabIndicator属性
            try {
                val slidingTabIndicatorField =
                    binding!!.tabLayout.javaClass.getDeclaredField("slidingTabIndicator")
                slidingTabIndicatorField.isAccessible = true
                val mTabStrip = slidingTabIndicatorField[binding!!.tabLayout] as LinearLayout
                LogUtils.i("tabLayout", mTabStrip.childCount.toString() + "   --")
                for (i in 0 until mTabStrip.childCount) {
                    val tabView = mTabStrip.getChildAt(i)
                    //拿到tabview的textview属性
                    val textViewField = tabView.javaClass.getDeclaredField("textView")
                    textViewField.isAccessible = true
                    val mTextView = textViewField[tabView] as TextView
                    tabView.setPadding(0, 0, 0, 0)

                    //控制Item条目的宽度，这种设置的话就是等分
                    val layoutParams = tabView.layoutParams as LinearLayout.LayoutParams
                    layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
                    tabView.layoutParams = layoutParams
                    tabView.invalidate()
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        //BlueToothManager.Companion.getInstance().release();
    }

    override fun onDestroyView() {
        super.onDestroyView()
        binding!!.viewPager.adapter = null
    }

    companion object {
        const val go_item = "go_item"
    }
}